package com.ruoyi.project.system.device.controller;

import java.util.concurrent.*;
 
public class BujijinChecker {

    private static final int MAX_ATTEMPTS = 20;
    private static final int CHECK_INTERVAL = 1000; // 间隔1000毫秒

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        double gas1Value = 1;
        double gas2Value = 10.6;
        double tolerance = 0.5;
        isValidT5(gas1Value, gas2Value, tolerance);
    }
    public static boolean isValidT5(double gas1Value, double gas2Value, double tolerance) throws ExecutionException, InterruptedException{
        Callable<Boolean> checkerTask = new Callable<Boolean>() {
            double finalGas1Value = gas1Value;
            @Override
            public Boolean call() throws Exception {
                boolean areConsistent = false;
                int attempts = 0;
                while (!areConsistent && attempts < MAX_ATTEMPTS) {
                    areConsistent = checkIfConsistent();
                    if (!areConsistent) {
                        finalGas1Value++;
                        Thread.sleep(CHECK_INTERVAL); // 等待指定时间进行下一次验证
                    }
                    attempts++;
                }
                return areConsistent;
            }

            private boolean checkIfConsistent() {
                // 这里应该是温度验证的逻辑
                // 返回温度是否一致的结果
                boolean isConsistent = false;
                if (Math.abs(finalGas1Value - gas2Value) < tolerance) {
                    isConsistent = true;
                } else {
                    isConsistent = false;
                }
                System.out.println(finalGas1Value);
                System.out.println(gas2Value);
                return isConsistent;
            }
        };

        ExecutorService executor = Executors.newSingleThreadExecutor();
        Future<Boolean> future = executor.submit(checkerTask);

        try {
            if (future.get()) {
                // 如果真空度一致，执行下一步操作
                return true;
//                nextStepOperation();
            }
        } finally {
            executor.shutdown(); // 关闭ExecutorService
        }
        return false;
    }
    private static void nextStepOperation() {
        System.out.println("真空度一致，可以进行下一步操作。");
        // 真空度一致后的操作
    }
}